﻿using ELab.Core;
using ELab.Core.Serialization.Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ELab.SystemWatcher
{
    public class ElabWatcher
    {
        #region Varial members
        private static ElabWatcher watcher = null;
        private string path2Watch = Path.Combine(SystemConfiguration.CurrentPath, SystemConfiguration.TransferFolder);
        #endregion

        public List<SynFileInfor> FileInfors
        {
            get
            {
                DirectoryInfo info = new DirectoryInfo(path2Watch);
                var infors = info.GetFiles("*.*");
                return infors.Select(x => new SynFileInfor
                {
                    FileName = x.FullName.Replace(path2Watch, string.Empty).Replace("\\",""),
                    FileLength = x.Length
                }).ToList();
            }
        }

        public byte[] ByteData
        {
            get
            {
                var info = FileInfors;
                if (info == null || info.Count == 0) return new byte[] { };
                return info.XmlSerializeToByte();
            }
        }

        #region Constructor
        public ElabWatcher(string path = null)
        {
            if (!string.IsNullOrWhiteSpace(path))
                path2Watch = path;
            if (!Directory.Exists(path2Watch)) Directory.CreateDirectory(path2Watch);
        }
        #endregion
    }
}
